<template>
	<!-- 管理员 -->
	<view class="">
		<view class="list" v-if="adminList.length>0">
			<view class="list-item" v-for="(item,index) in adminList" :key="index">
				<view class="list-item-left">
					<image class="lil-avatar" :src="'https://guizhouchenxun.com/uploads/'+item.user.avatar" mode=""></image>
					<view class="lil-info">
						<view class="lil-name">
							{{item.user.nickname}}
						</view>
						<view class="lil-phone">
							{{item.user.phone}}
						</view>
					</view>
				</view>
				<view class="list-item-right">
					<button @click="deleteAdmin(item)">删除</button>
				</view>
			</view>
		</view>
		<view class="isNull" v-if="adminList.length==0">
			<image src="@/static/nodata.png" mode=""></image>
			<text>暂时没有数据~</text>
		</view>
		<view class="btn">
			<view class="btn-left">
				共<text>{{adminList.length}}</text>位
			</view>
			<view class="btn-right">
				<button @click="show = true">添加管理员</button>
			</view>
		</view>
		<!-- 添加弹窗 -->
		<view class="cu-modal" :class="show?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">添加管理员</view>
					<view class="action" @tap="show = false">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="popup">
					<view class="popup-header">
						<view class="popup-header-lable">
							搜索用户:
						</view>
						<view class="popup-header-input">
							<input v-model="inputValue" type="text" placeholder="请输入用户完整手机号">
						</view>
						<view class="popup-header-btn">
							<button @click="selectUser()">搜索</button>
						</view>
					</view>
					<view class="popup-user">
						<view class="popup-user-item" v-for="(item,index) in userList" :key="index">
							<image @click="userIndex = index" :class="index==userIndex?'user-active':''" :src="'https://guizhouchenxun.com/uploads/'+item.avatar" mode=""></image>
							<view class="pui-name">
								<text>{{item.nickname}}</text>
								<text>{{item.phone}}</text>
							</view>
						</view>
					</view>
					<view class="popup-btn">
						<button @click="addAdmin()">确认</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {getAdminListApi,addAdminApi,getUserListApi,deleteAdminApi } from "@/common/api.js"
	export default{
		data(){
			return{
				show: false,
				inputValue: '', //搜索内容
				userIndex: 0,  //选中下标
				userList: [],
				adminList:[],  //管理员列表
			}
		},
		onLoad() {
			this.getAdminList()
		},
		methods:{
			// 获取管理员列表
			getAdminList(){
				getAdminListApi().then(res=>{
					console.log(res)
					this.adminList = res.data
					console.log("获取管理员列表",this.adminList)
				})
			},
			// 根据手机号搜索用户列表
			selectUser(){
				this.userList = []
				getUserListApi({phone:this.inputValue}).then(res=>{
					if(res.code == 200001){
						this.userList.push(res.data)
					}else{
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
				})
			},
			// 添加管理员
			addAdmin(){
				console.log(this.userList[this.userIndex])
				addAdminApi({phone:this.userList[this.userIndex].phone}).then(res=>{
					if(res.code == 200001){
						this.inputValue = ''
						this.userList = []
						uni.showToast({
							title: '添加成功',
							icon: 'none'
						})
						this.show = false
						this.getAdminList()
					}else{
						uni.showToast({
							title: res,
							icon: 'none'
						})
					}
				})
			},
			// 删除管理员
			deleteAdmin(item){
				let than = this
				uni.showModal({
					content: '是否要删除该管理员，删除过后该用户将无法继续对店铺订单进行核销',
					success(e) {
						if(e.confirm){
							deleteAdminApi({id:item.id}).then(res=>{
								if(res.code==200001){
									uni.showToast({
										title: '删除成功',
										icon: 'none'
									})
									than.adminList.forEach((i,k)=>{
										if(i.id = item.id){
											than.adminList.splice(k,1)
										}
									})
								}else{
									uni.showToast({
										title: res.message,
										icon: 'none'
									})
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.list{
		margin: 0 30rpx;
	}
	
	.list-item{
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx;
		background: #fff;
		border-radius: 10rpx;
		margin-top: 30rpx;
	}
	
	.list-item-left{
		display: flex;
		align-items: center;
	}
	
	.lil-avatar{
		height: 120rpx;
		width: 120rpx;
		flex-shrink: 0;
		border-radius: 10rpx;
		margin-right: 20rpx;
	}
	
	.lil-name{
		font-size: 32rpx;
	}
	
	.lil-phone{
		margin-top: 20rpx;
		font-size: 26rpx;
	}
	
	.list-item-right{
		button{
			height: 60rpx;
			width: 120rpx;
			font-size: 26rpx;
			color: red;
		}
	}
	
	.btn{
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 99;
		width: 100%;
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		background: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	
	.btn-left{
		font-size: 28rpx;
		text{
			font-size: 34rpx;
			color: red;
			margin: 0 10rpx;
		}
	}
	
	.btn-right{
		button{
			width: 300rpx;
			height: 70rpx;
			line-height: 70rpx;
			font-size: 32rpx;
			background: #3c9cff;
			border: 1rpx solid #3c9cff;
		}
	}
	
	.popup{
		padding: 30rpx;
		background: #fff;
	}
	
	.popup-header{
		display: flex;
		align-items: center;
	}
	
	.popup-header-lable{
		font-size: 28rpx;
		flex-shrink: 0;
	}
	
	.popup-header-input{
		width: 100%;
		input{
			text-align: left;
			padding: 0 20rpx;
			width: 100%;
			font-size: 28rpx;
		}
	}
	
	.popup-header-btn{
		button{
			width: 120rpx;
			height: 60rpx;
			line-height: 60rpx;
			font-size: 26rpx;
			border: 1rpx solid #e0e0e0;
		}
	}
	
	.popup-user{
		margin-top: 30rpx;
		min-height: 260rpx;
		background: #f5f5f5;
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
		align-content: flex-start;
		padding: 30rpx 0;
		box-sizing: border-box;
	}
	
	.popup-user-item{
		height: 100rpx;
		// width: calc(100% / 4);
		border-radius: 10rpx;
		box-sizing: border-box;
		border: 1rpx solid #f5f5f5;
		margin-bottom: 30rpx;
		display: flex;
		align-items: center;
		image{
			height: 100rpx;
			width: 100rpx;
			flex-shrink: 0;
			border-radius: 10rpx;
			margin-right: 20rpx;
		}
	}
	
	.pui-name{
		height: 100rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}
	
	.pui-name>text:first-child{
		font-size: 32rpx;
		text-align: left;
	}
	
	.pui-name>text:last-child{
		font-size: 28rpx;
	}
	
	.user-active{
		border: 1rpx solid red;
	}
	
	.popup-btn{
		margin-top: 30rpx;
		button{
			width: 100%;
			height: 80rpx;
			line-height: 80rpx;
			background: #3c9cff;
		}
	}
	
	.isNull{
		margin-top: 200rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		image{
			width: 410rpx;
			height: 240rpx;
			flex-shrink: 0;
		}
		text{
			margin-top: 30rpx;
			font-size: 28rpx;
			color: #999;
		}
	}
</style>